package com.emode.action;



import java.util.Date;
import com.emode.base.BaseAction;
import com.emode.entity.Bbsreply;
import com.emode.entity.Bbstopic;
import com.emode.entity.Bbsusers;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author Administrator
 *
 */
@SuppressWarnings("unchecked")
public class ReplyAction extends BaseAction<Bbsreply>{
	/**
	 * 
	 */
	private static final long serialVersionUID = 5252747771270005692L;
	public String view(){
		if(null ==topic){
			System.out.println("topicId 为空");
			return null;
		}
		//不管有没有回复 主题还是要出来的
		System.out.println("view-->topicId="+topic.getTid());
		Bbstopic list = topicservice.getById(topic.getTid());
		//增加一个回复数
		topicservice.updateClickCount(topic.getTid());
		//replyservice.findObjectByConditionWithPage(whereHql, values, orderby, p);
		int count = replyservice.countReply(topic.getTid());
		p.init(count);
		list2 = replyservice.listreply(topic.getTid(),p);
		ActionContext.getContext().put("t", list);
		ActionContext.getContext().put("r", list2);
		return "replylist";
	
	}
	
	// 添加 回复
	public String addreply(){
		if(model.getRcontent().trim().length()<3){
			msg = "回复字符不能少于两个";
			return "replylist";
		}
		model.setBbssection(section);
		model.setBbstopic(topic);
		model.setBbsusers(users);
		model.setRtime(new Date());
		/*String nameLink = regexContent.NameLinks(reply.getRcontent());//得到过滤的用户名
		try {
			createNotice(nameLink);
		} catch (Exception e) {
			System.out.println(e);
			e.printStackTrace();
		}// 添加提醒
*/		
		replyservice.save(model);
		topic.setTlastReply(new java.sql.Timestamp(new java.util.Date().getTime()));
		topicservice.updateTopic(topic);
		System.out.println("toview--> topicid:"+topic.getTid());
		topic.setTid(topic.getTid());
		return "toreplylist";
		
	}
	/*
	 * 创建消息提醒
	 */
	public void createNotice(String name) throws Exception{
		Bbsusers user = new Bbsusers();
		user.setUname(name);
		user = userservice.find(user);
		if(user!=null){
		/*	Bbsusers user2 =(Bbsusers) list.get(0);
			System.out.println("user.uid="+user.getUid()+"users.uid="+users.getUid());
			unotice.setBbsusersBySuid(users);
			unotice.setBbsusersByNuid(user);
			unotice.setContent("xxx回复了你");
			unotice.setNtime(new Date());
			Short s = 1;
			unotice.setNtype(s);
			noservice.update(unotice);
			System.out.println(11);*/
		}
	}

}
